/**
 * Copyright (c) 2011 Roundarch (www.roundarch.com)
 *
 * Author Joshua Jung
 *
 * ---
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.roundarch.flex.controls.intervals
{
    import com.roundarch.flex.controls.graphControlInterfaces.IInterval;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IEventDispatcher;

    /**
     *
     * @author jjung
     */
    public class Interval extends EventDispatcher implements IInterval
    {
        //---------------------------------------------------------------------
        //
        //  Variables
        //
        //---------------------------------------------------------------------
        protected var binders : Array = [];

        //---------------------------------------------------------------------
        //
        //  Properties
        //
        //---------------------------------------------------------------------
        //-----------------------------
        // start
        //-----------------------------
        /**
         * @private
         */
        protected var _start : Number = 0;

        [Bindable("startChanged")]
        /**
         *
         *
         * @default 0
         */
        public function get start() : Number
        {
            return _start;
        }

        /**
         * @private
         */
        public function set start(value : Number) : void
        {
            if (value == _start)
            {
                return;
            }

            _start = value;

            notifyBinders();

            dispatchEvent(new Event("startChanged"));
        }

        //-----------------------------
        // end
        //-----------------------------
        /**
         * @private
         */
        protected var _end : Number = 100;

        [Bindable("endChanged")]
        /**
         *
         *
         * @default 100
         */
        public function get end() : Number
        {
            return _end;
        }

        /**
         * @private
         */
        public function set end(value : Number) : void
        {
            if (value == _end)
            {
                return;
            }

            _end = value;

            notifyBinders();

            dispatchEvent(new Event("endChanged"));
        }

        //-----------------------------
        // length
        //-----------------------------
        public function get length() : Number
        {
            if (start > end)
            {
                throw new Error("Invalid start and end dates. Start is after end!");
            }

            return end - start;
        }

        //---------------------------------------------------------------------
        //
        //  Constructor
        //
        //---------------------------------------------------------------------
        /**
         *  Constructor
         */
        public function Interval(start : Number = 0, end : Number = 100) : void
        {
            this._start = start;
            this._end = end;
        }

        //---------------------------------------------------------------------
        //
        //  Methods
        //
        //---------------------------------------------------------------------
        public function bind(ed : IEventDispatcher, event : String) : void
        {
            if (ed)
            {
                binders.push([ed, event]);
            }
        }

        protected function notifyBinders() : void
        {
            for each (var binder : Array in binders)
            {
                (binder[0] as IEventDispatcher).dispatchEvent(new Event(binder[1]));
            }
        }
    }
}
